﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ParseData
{
    /// <summary>
    /// File reading utility class.
    /// </summary>
    public class FileReaderUtil
    {

        private FileReaderUtil()
        {
        }

        /// <summary>
        /// Read the contents of the file and return an array of lines
        /// </summary>
        /// <param name="FileName">Location of the file.</param>
        public static string[] SplitFileByLine(string FileName)
        {
            StreamReader fileReader = null;
            try
            {
                // Read the contents of the entire file
                fileReader = new StreamReader(FileName);
                StringBuilder fileContents = new StringBuilder();
                char[] buffer = new char[32768];
                while (fileReader.ReadBlock(buffer, 0, buffer.Length) > 0)
                {
                    fileContents.Append(buffer);
                    buffer = new char[32768];
                }

                // Separate the contents of the file into lines
                fileContents = fileContents.Replace("\r\n", "\n");
                return fileContents.ToString().Split('\n');
            }
            catch (Exception ex)
            {
                throw (new IOException("Unable to read " + FileName, ex));
            }
            finally
            {
                if (fileReader != null)
                {
                    fileReader.Close();
                }
            }
        }

    }
}
